<?php

/* --|	COMMENT
  Software: Socially
  Version: 1.0
  Contact: http://socially.googlecode.com
  Info: http://socially.googlecode.com
  Support: http://socially.googlecode.com
  --------------------------------------------------------------------------
  Author: goker.cebeci | http://gokercebeci.com
  --------------------------------------------------------------------------
  License: Distributed under the GNU General Public License (GPL)
  http://www.gnu.org/licenses/gpl.html
  This program is distributed in the hope that it will be useful - WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.
  ------------------------------------------------------------------------- */

class comment {

    public static function post($a) {
        if (!$a['app'])
            return common::result(800, _('Requered value is empty'));
        if (!$a['itemID'])
            return common::result(800, _('Requered value is empty'));
        if (!$a['senderID'])
            return common::result(800, _('Requered value is empty'));
        if (!$a['comment'])
            return common::result(400, _('Please enter a comment'), array('comment'));
        $r = db::query(sprintf("INSERT INTO postieComments (`app`,`itemID`,`senderID`,`comment`,`date`)
                    VALUES ('%s','%d','%d','%s',NOW());",
                                $a['app'], $a['itemID'], $a['senderID'], $a['comment']));
        cache::del('postie[comment][' . $a['app'] . ']][' . $a['itemID'] . ']');

        return db::insertid(); //common::result(200,_('Your comment has been sent.'));
    }

    public static function delete($a) {
        if (!$a['ID'])
            return common::result(800, _('Requered value is empty'));
        $r = db::query(sprintf("DELETE FROM postieComments WHERE `ID` = %d", $a['ID'])
                        . (EDITOR ? ';' : sprintf(" AND `accountID` = %d;", UID)));
        //cache::del('postie[comment]['.$a['app'].']]['.$a['itemID'].']');
        return common::result(200, 'comment' . $a['ID']);
    }

    public static function thread($a) {
        $a['limit'] = $a['limit'] && $a['limit'] < 30 ? $a['limit'] : 30;
        $r = cache::get('postie[comment][' . $a['app'] . ']][' . $a['itemID'] . ']');
        if (!$r) {
            $r = db::query(sprintf("SELECT * FROM postieComments WHERE app = '%s' AND itemID = %d ORDER BY `date` DESC LIMIT %d;"
                                    , $a['app'], $a['itemID'], $a['limit'] + 1), true, true);
            if ($r)
                foreach ($r as $k => $i) {
                    $r[$k]->sender = profile::get(array('ID' => $i->senderID));
                }
            cache::set('postie[comment][' . $a['app'] . ']][' . $a['itemID'] . ']', $r);
        }
        return $r;
    }

}

?>